package com.yf.dialog;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void alert(View v) {
		// 创建一个AlertDialog.Builder(对话框构建对象)
		// 参数必须为一个Activity实例，而不能是getApplicationContext()
		// 因为对话框作为当前Activity的一部分，而不是一个新的Activity
		// getApplicationContext()返回的是应用的上下文对象而不是一个Activity实例
		// Activity是Context的子类
		// Activity继承了ContextThemeWrapper，ContextThemeWrapper继承了ContexWrapper,ContexWrapper继承了Context
		Builder builder = new Builder(this);
		builder.setTitle("普通对话框");
		builder.setMessage("这是一个普通对话框");
		builder.setPositiveButton("确定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// Toast是系统级的UI控件，
				Toast.makeText(getApplicationContext(), "您点击了确定", Toast.LENGTH_SHORT).show();
			}

		});

		builder.setNegativeButton("取消", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "您点击了取消", Toast.LENGTH_SHORT).show();
			}
		});

		builder.show();
	}

	public void singleChoice(View v) {
		Builder builder = new Builder(this);
		builder.setTitle("单选对话框");
		final String[] items = { "丹尼斯", "詹姆斯", "本贾尼", "吉多", "布兰登" };
		builder.setSingleChoiceItems(items, 4, new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
				dialog.dismiss();
			}
		});
		builder.show();
	}

	public void multiChoice(View v) {
		Builder builder = new Builder(this);
		builder.setTitle("单选对话框");
		final String[] items = { "丹尼斯", "詹姆斯", "本贾尼", "吉多", "布兰登" };
		boolean[] checkedItems = new boolean[] {true, false, false, false, false};
		builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				Log.v("Dialog.multiChoice", items[which] + (isChecked ? " 选中" : " 取消选中"));
			}

		});
		builder.setPositiveButton("确定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.show();
	}
	
	public void progress(View v) {
		final ProgressDialog dialog = new ProgressDialog(this);
		dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		dialog.setTitle("正在玩命下载中...");
		dialog.setMax(100);
		// 下载开始显示进度条
		dialog.show();
		// 耗时操作必须另开一个子线程运行
		new Thread() {
			public void run() {
				for (int i=0; i<=100; i++) {
					// 显示操作进度的UI可以直接在子线程操作
					dialog.setProgress(i);
					SystemClock.sleep(100);
				}
				// 下载完成销毁进度条
				dialog.dismiss();
			};
		}.start();
	}
	
}
